|
|
|
|
|
|
|
The C declaration for the structure suggests that the dwOSVersionInfoSize field contains the size of the structure. Does this mean that the API function loads the structure with the size or that you have to set the size before calling the function? If you followed the hint provided for this puzzle in Appendix A of this book and read the documentation for the function carefully, you will have noticed a brief comment explaining that the dwOSVersionInfoSize field must be initialized to the size of the structure before calling the function.1 |
|
|
|
|
|
|
|
|
The following simple code modification solves the problem: |
|
|
|
|
|
|
|
|
' You have to initialize the size field
os.dwOSVersionInfoSize = Len(os) |
|
|
|
|
|
|
|
|
Why does Windows require you to initialize the structure size? Shouldn't it already know how big the structure is? |
|
|
|
|
|
|
|
|
The answer is subtle and elegant. Windows uses the size to determine the version of the structure the calling application expects to see. Let's say the Windows developers decide that, for Windows NT 8.0, they want to add a new 128-character string that would allow you to retrieve Bill Gates's e-mail address so he could provide you with personal technical support. Older applications would still use the current OSVERSIONINFO structure, and if the system tried to write data into this new buffer, the application would almost certainly crash. This way, the system can look at the size of the buffer. The current OSVERSIONINFO structure is 148 bytes long; the new one would be 276 bytes. |
|
|
|
|
|
|
|
|
Windows uses this technique extensively with structures, so if you ever find yourself with a function that uses a structure parameter and fails to work, make sure that any required fields are initialized. |
|
|
|
 |
|
 |
|
|
1 My technical editor suggested that it might be unfair to expect readers to have access to my API book or the Win32 SDK online documentation. I responded that this book is useless to anyone who doesn't have access to the Win32 API documentation anyway, so of course they'll have access to it. |
|
|
|
 |
|
 |
|
|
He then suggested that it was unfair to offer nothing more than a hint in an appendix to point you in the right direction and especially unfair not to include the information somewhere in the puzzle itself. In this he is correct. |
|
|
|
 |
|
 |
|
|
My message to you is this: If the only thing you learn from this puzzle is the need to refer back to the actual API documentation and read it carefully, this puzzle has more than served its purpose. |
|
|
|
|
|